﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Collections;
using System.Windows.Threading;
using System.Globalization;
using Microsoft.Advertising.Mobile.UI;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
using OnlineVideos.ViewModels;
using System.ComponentModel;
//using Helper;
using Common.Library;
using OnlineVideos.Entities;
using OnlineVideos.Common;
using OnlineVideos;
using OnlineVideos.Data;

using Common;
using Common.Utilities;
using System.Threading.Tasks;

namespace OnlineVideos.Views
{
    public partial class CastPanorama : PhoneApplicationPage
    {
        #region GlobalDeclaration

        private SolidColorBrush adcontrolborder = new SolidColorBrush(Colors.Transparent);
        private bool IsDataLoaded;
        string type = "";
        int ToggleBackgroundFlag = 0;
        string ReadText = string.Empty;
        #endregion

        #region Constructor
        public CastPanorama()
        {
            InitializeComponent();
            PersonPanorama.Background = LoadCastPanoramaBackground(AppSettings.PersonID);
            IsDataLoaded = false;
        }

        private Brush LoadCastPanoramaBackground(string p)
        {
            ImageBrush panoramabackground = new ImageBrush();
            string path = "";
            int personid = Convert.ToInt32(p);
            CastProfile Cast = new CastProfile();
            var topRatedList = Task.Run(async () => await Constants.connection.Table<CastProfile>().Where(i => i.PersonID == personid).ToListAsync()).Result;
            foreach (CastProfile itm in topRatedList)
            {
                path = itm.FlickrPanoramaImageUrl;
            }
            if (path != null && path != "")
            {
                BitmapImage PersonPanoramaImage = new BitmapImage();
                PersonPanoramaImage.UriSource = new Uri(path, UriKind.Absolute);
                panoramabackground.ImageSource = PersonPanoramaImage;
            }


            return panoramabackground;
        }
        #endregion

        #region PageLoad

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            PageHelper.LoadAdControl(LayoutRoot, adstackPsnProfile, 1);
            FlurryWP8SDK.Api.LogEvent("CastPanorama Page", true);
            Constants.Synthesizer = null;
            if (IsDataLoaded == false)
            {
                BackgroundHelper bwHelper = new BackgroundHelper();
                bwHelper.AddBackgroundTask(
                                            (object s, DoWorkEventArgs a) =>
                                            {

                                                a.Result = OnlineShow.GetPersonDetail(AppSettings.PersonID).Description;
                                            },
                                            (object s, RunWorkerCompletedEventArgs a) =>
                                            {
                                                ReadText = a.Result.ToString();
                                                if (ReadText != null)
                                                {
                                                    imageSpeech.Visibility = Visibility.Visible;
                                                }

                                            });
                bwHelper.RunBackgroundWorkers();

                CastPanoramaTitle.Text = OnlineShow.GetPersonDetail(AppSettings.PersonID).Name;
                IsDataLoaded = true;

            }
            _performanceProgressBar.IsIndeterminate = false;

        }
        #endregion

        #region "Common Methods"
       
        public ImageBrush BlankBackground()
        {
            ImageBrush PanoramBrush = new ImageBrush();
            PanoramBrush = null;
            return PanoramBrush;
        }
        
        #endregion

        #region Events
        private void imageSpeech_Click(object sender, MouseButtonEventArgs e)
        {
            try
            {
            if (Constants.Synthesizer == null)
            {
                Constants.Synthesizer = new Windows.Phone.Speech.Synthesis.SpeechSynthesizer();
                Task.Run(async () => await Constants.Synthesizer.SpeakTextAsync(ReadText));
                imageSpeech.Source = ResourceHelper.StopSpeech;
            }
                else
                {
                imageSpeech.Source = ResourceHelper.StartSpeech;
                Constants.Synthesizer.Dispose();
                Constants.Synthesizer = null;
                }
       
            }
            catch (Exception ex)
            {
         
                Exceptions.SaveOrSendExceptions("Exception in imageSpeech_Click Method In CastPanorama.cs file.", ex);
        }
         
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            _performanceProgressBar.IsIndeterminate = true;
            string pid = "";
            string Mid = "";
            if (NavigationContext.QueryString.TryGetValue("type", out type))

                if (NavigationContext.QueryString.TryGetValue("Mid", out Mid))

                    if (NavigationContext.QueryString.TryGetValue("pid", out pid))
                    {
                        if (type == "search")
                        {
                            PersonPanorama.DefaultItem = PersonPanorama.Items[2];
                        }
                    }

            if (type == "search")
            {
                NavigationService.RemoveBackEntry();
            }
        }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In CastPanorama.cs file.", ex);
            }
        }
        private void ToggleBackground_Click(object sender, MouseButtonEventArgs e)
        {
            try
            {
            if (ToggleBackgroundFlag == 1)
            {
                PersonPanorama.Background = LoadCastPanoramaBackground(AppSettings.PersonID);
                ToggleBackgroundFlag = 0;
            }
            else
            {
                PersonPanorama.Background = new SolidColorBrush(Colors.Black);
                ToggleBackgroundFlag = 1;
            }
        }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in ToggleBackground_Click Method In CastPanorama.cs file.", ex);
            }
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (AppSettings.bcount)
            {
                if (NavigationService.CanGoBack)
                    NavigationService.GoBack();
            }
            else
                AppSettings.detailtocast = 0;
        }
       
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("CastPanorama Page");
            }
            catch (Exception ex)
            {

                  Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In CastPanorama.cs file.", ex);
            }
        }
       
        #endregion

    


    }
}